<?php

/**
 * All functions related to displaying current user's messages on his home page.
 */
class Model_Home {
   
   /**
    * Get all posts written by the current user.
    * @param int $id The current user's ID.
    * @return array All posts written by the current user.
    * @author Christophe Sourisse
    */
   public function getUserPosts($id) {
      $db = Zend_Registry::get("my_db");
      $sql = 'SELECT * FROM messages WHERE author=?';
      $result = $db->fetchAll($sql, $id);
      return $result;
   }
   
   /**
    * Get all posts written by the current user, sorted from the most recent to
    * the latest.
    * @param int $id The current user's ID
    * @return array A sorted array with all posts written by the current user
    * @author Christophe Sourisse
    */
   public function getUserPosts_sorted($id) {
      $posts_to_sort = self::getUserPosts($id);
      $map = self::getMapTimeMessages($posts_to_sort);
      $sorted_posts = self::sortMessages($posts_to_sort, $map);
      return $sorted_posts;
   }
   
   /**
    * Get all posts written by visitors on a user's page.
    * @param int $id The current user's ID.
    * @return array All posts written on the current user's page
    * @author Dominik Imsirovic
    */
   private function getVisitorPosts($id) {
      $db = Zend_Registry::get("my_db");
      $sql = 'SELECT * FROM messages WHERE topic=?';
      $result = $db->fetchAll($sql, $id);
      return $result;
   }
   
   /**
    * Get all posts written on the current user's page, sorted from the most recent to
    * the latest.
    * @param int $id The current user's ID
    * @return array A sorted array with all posts on the current user's page
    * @author Dominik Imsirovic
    */
   public function getVisitorPosts_sorted($id) {
      $posts_to_sort = self::getVisitorPosts($id);
      $map = self::getMapTimeMessages($posts_to_sort);
      $sorted_posts = self::sortMessages($posts_to_sort, $map);
      return $sorted_posts;
   }
   
   /**
    * Get all new messages of a specific topic.
    * @param int $id The desired topic's ID.
    * @return array All new messages from the desired topic.
    */
   public function getNewMessages_topic($id) {
      $db = Zend_Registry::get("my_db");
      $sql = 'SELECT * FROM messages WHERE topic=?';
      $result = $db->fetchAll($sql, $id);
      return $result;
   }
   
   /**
    * Get all new messages of the topics the current user subscribed to.
    * @param string $topics The topics' IDs this user subscribed to.
    * @return array All new messages from the topics this user subscribed to.
    */
   public function getNewMessages($topics) {
      // Get an array of all topics' indexes.
      $topics_array = preg_split("#,#", $topics);
      
      // Get an array with all topics' messages.
      $messages_array = array();
      if (strlen($topics) > 0) {
         foreach ($topics_array as $topicID) {
            $messages_array = array_merge($messages_array, self::getNewMessages_topic($topicID));
         }
      }
      
      return $messages_array;
      
   }
   
   /**
    * Get a mapping between each message of the given array and its date of creation.
    * Note: the returned map is sorted from the biggest to the lowest "poids".
    * @param type $messages_array
    * @return array An array mapping each message ID to an integer. The biggest 
    * this integer is, the more recent the message is.
    */
   public function getMapTimeMessages($messages_array) {
      
      // Define the time zone.
      date_default_timezone_set('Europe/Paris');
      
      // For each message, generate a time integer.
      $map_array = array();
      foreach ($messages_array as $message) {
         $map_array[$message["id"]] = mktime(
                 substr($message["date"], 11, 2), //hour
                 substr($message["date"], 14, 2), //minute
                 substr($message["date"], 17, 2), //second
                 substr($message["date"], 5, 2), //month
                 substr($message["date"], 8, 2), //day
                 substr($message["date"], 0, 4) //year
         );
      }
      
      // Sort the messages with reverse chronological order.
      arsort($map_array);
      return $map_array;
      
   }
   
   /**
    * Sort messages according to a map.
    * @param array $messages - the messages to be sorted
    * @param array $map - the map (computed with the 'getMapTimeMessages()' function)
    * @return array - the given messages, sorted accordingly to the map
    */
   public function sortMessages($messages, $map) {
      $sorted_messages = array();
      foreach ($map as $key => $value) {
         $id = 0;
         while ($id<count($messages) & $key!=$messages[$id]["id"]) {
            $id++;
         }
         if ($id != count($messages))
            $sorted_messages[] = $messages[$id];
      }
      return $sorted_messages;
   }
   
   /**
    * * Get all new messages of the topics the current user subscribed to, sorted by date.
    * @param string $topics The topics' IDs this user subscribed to.
    * @return array All new messages from the topics this user subscribed to, sorted by date.
    */
   public function getNewMessages_sorted($topics) {
      $messages_array = self::getNewMessages($topics);
      $map = self::getMapTimeMessages($messages_array);
      $sorted_messages = self::sortMessages($messages_array, $map);
      return $sorted_messages;
   }
   
   
}
